/**
 * 
 */
package com.wodsy.phoenix.datadao.cms.dao.impl;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.cms.dao.CmsHomePageCustomizePicDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.cms.constants.CmsContentStatus;
import com.wodsy.phoenix.dataobject.cms.model.IdxCustomizePic;

/**
 * @author bryan
 *
 */
@Repository
public class CmsHomePageCustomizePicDaoImpl extends BaseHibernateDao<IdxCustomizePic, Integer>
		implements CmsHomePageCustomizePicDao {
	@Autowired
    @Qualifier(value="cms")
	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Class<IdxCustomizePic> getModelClass() {
		return IdxCustomizePic.class;
	}

	@Override
	public IdxCustomizePic getOnlineCustomizePicRec() {
		Criteria criteria = getSession().createCriteria(getModelClass())
		.add(Restrictions.eq("status", CmsContentStatus.ISONLINE.getKey()));
		criteria.setMaxResults(1);
		return (IdxCustomizePic)criteria.uniqueResult();
	}

	@Override
	public Pager<IdxCustomizePic> searchHotGoodsRecByOrder(
			Pager<IdxCustomizePic> picRec, IdxCustomizePic picreq) {
		Criteria criteria = getSession().createCriteria(getModelClass());
		if(!StringUtils.isEmpty(picreq.getPicAlt())){
			criteria.add(Restrictions.like("picAlt", "%"+picreq.getPicAlt()+"%"));
	    }
		if(!StringUtils.isEmpty(picreq.getStatus())) {
			criteria.add(Restrictions.eq("status", picreq.getStatus()));
		}
		if(!StringUtils.isEmpty(picreq.getOnlineTime())&&!StringUtils.isEmpty(picreq.getOfflineTime())) {
			criteria.add(Restrictions.le("onlineTime", picreq.getOnlineTime()));
			criteria.add(Restrictions.ge("offlineTime", picreq.getOfflineTime()));
		}else if(!StringUtils.isEmpty(picreq.getOnlineTime())){
			criteria.add(Restrictions.le("onlineTime", picreq.getOnlineTime()));
			criteria.add(Restrictions.ge("offlineTime", picreq.getOnlineTime()));
		}
        criteria.addOrder(Property.forName("status").asc());
        return toPager(picRec, criteria);
	}

	@Override
	public Integer updateCustomizePicStatus(int picId, int status) {
		Session session = getSession();
		String hqlUpdate =
				"update IdxCustomizePic t set t.status = :status,t.updateTime= :updateTime where t.id = :picId";
		int updatedEntities = session.createQuery( hqlUpdate )
			.setInteger("status", status)
			.setTimestamp("updateTime", new Date())
			.setInteger("picId", picId)
			.executeUpdate();
		return updatedEntities;
	}

	@Override
	public Integer updateCustomizePicStatus(Integer[] picIds, int status) {
		Session session = getSession();
		String hqlUpdate =
				"update IdxCustomizePic t set t.status = :status,t.updateTime= :updateTime where t.id in (:picIds)";
		int updatedEntities = session.createQuery( hqlUpdate )
			.setInteger("status", status)
			.setTimestamp("updateTime", new Date())
			.setParameterList("picIds", picIds)
			.executeUpdate();
		return updatedEntities;
	}

	@Override
	public Integer addCustomizePicRecommended(IdxCustomizePic customizePic) {
		Session session = getSession();
		session.save(customizePic);
		return customizePic.getId();
	}

	@Override
	public void updateCustomizePicRecommended(IdxCustomizePic customizePic) {
		Session session = getSession();
		session.update(customizePic);		
	}



}
